<?php
/**
 * 分类管理
 */
namespace app\admin\controller;
class Sorting extends Admin {

	public function index() {
		//查询所有分类
		$mSort = D('api/Sorting');
		$list = $mSort->where(['tmp' => 1])->order('sort desc,id asc')->select();
		$list = $mSort->typeArray($list);
		return $this->fetch('index', [
			'list' => $list,
		]);
	}

	//分类添加
	public function create() {
		$mSort = M('Sorting');
		if (IS_POST) {
			$post = I('post.');
			$result = $this->validate($post, 'Sorting');
			if ($result !== TRUE) {
				$res['status'] = 'n';
				$res['info'] = $result;
			} else {
				$post['tmp'] = 1;
				$res['status'] = $mSort->where(['id' => I('post.id')])->setField($post) ? 'y' : 'n';
				if ($res['status'] == 'n') {
					$res['info'] = '添加数据库失败！';
				}
			}
			return json_encode($res);
		} else {
			$list = $mSort->where(['pid' => 0, 'tmp' => 1])->order('sort desc,id asc')->select();
			//查询是否有临时数据
			$tmpSort = $mSort->where(['tmp' => 2])->find();
			if (!$tmpSort) {
				$data = [
					'pid' => 0,
					'update_time' => time(),
					'tmp' => 2,
					'image' => 'http://7xr2re.com1.z0.glb.clouddn.com/error.png',
					'sname' => '',
					'sort' => 0,
				];
				$data['id'] = $mSort->add($data);
				$tmpSort = $data;
			}
			return $this->fetch('create', [
				'list' => $list,
				'res' => $tmpSort,
				'status' => 'create',
			]);
		}
	}

	//修改
	public function update() {
		$mSort = M('Sorting');
		if (IS_POST) {
			$post = I('post.');
			//查询类名重复
			$result = $mSort->where(['id' => ['neq', $post['id']], 'sname' => $post['sname']])->find();
			if ($result) {
				return json_encode(['status' => 'n', 'info' => '该分类名称已经存在']);
			}

			$result = $mSort->validate(
				[
					'sname' => 'require|max:25',
					'sort' => 'number|between:0,999',
				],
				[
					'sname.require' => '请填写分类名称',
					'sname.max' => '分类名称最多只能有25个字符',
					'sort' => '分类排序必须为数字类型',
					'sort.between' => '分类排序必须为小于999的正整数',
				]
			)->create($post);
			if (!$result) {
				$res['status'] = 'n';
				$res['info'] = $mSort->getError();
			} else {
				$post['tmp'] = 1;
				$res['status'] = $mSort->where(['id' => I('post.id')])->setField($post) ? 'y' : 'n';
				if ($res['status'] == 'n') {
					$res['info'] = '添加数据库失败！';
				}
			}
			return json_encode($res);
		} else {
			$sid = I('get.sid/d');
			$res = $mSort->where(['id' => $sid])->find();
			if (!$res['id']) {
				return $this->error('该分类不存在');
			} else {
				$list = $mSort->where(['pid' => 0, 'tmp' => 1])->order('sort desc,id asc')->select();
				return $this->fetch('create', [
					'res' => $res,
					'list' => $list,
					'status' => 'update',
				]);
			}
		}
	}

	//删除
	public function delete() {
		$sid = I('post.sid');
		$mSort = D('api/Sorting');
		if (is_array($sid)) {
			foreach ($sid as $v) {
				$res = $mSort->get_junior($v);
			}
		} else {
			$res = $mSort->get_junior($sid);
		}
		if ($res) {
			return json_encode(['status' => 'y']);
		} else {
			return json_encode(['status' => 'n', 'info' => '添加数据失败！']);
		}
	}

	//分类图片上传
	public function upload() {
		$auth = new \Qiniu\Auth(C('AccessKey'), C('SecretKey'));
		$upToken = $auth->uploadToken(C('bucket'));

		$uploadMgr = new \Qiniu\Storage\UploadManager();
		//自定义文件名
		$key = 'sorting_' . I('post.sid') . '.png';

		//删除已经存在的图片
		$bucketMgr = new \Qiniu\Storage\BucketManager($auth);
		$bucketMgr->delete(C('bucket'), $key);

		list($ret, $err) = $uploadMgr->putFile($upToken, $key, $_FILES['file']['tmp_name']);

		if ($err) {
			return '失败！';
		} else {
			//加入数据库
			$url = C('qn_url') . $ret['key'];
			$mSort = M('sorting');
			return $mSort->where(['id' => I('post.sid/d')])->setField('image', $url);
		}
	}

}
